import datetime
import os
import subprocess
import paramiko
start = datetime.datetime.now()
project_path = r'E:\project\yun-zhong-ke'
os.chdir(project_path)

res = os.system('yarn run build')
print(res, '打包前端项目完成')

zip_cmd = [r"D:\7-Zip\7z.exe", "a", "-tzip", 'dist.zip', 'dist']

res = subprocess.run(zip_cmd, shell=True)
print(res, '压缩前端项目完成')

scp_cmd = ['scp', 'dist.zip', 'xb:/project/yun-zhong-ke/']
res = subprocess.run(scp_cmd, shell=True)
print(res, '上传前端项目完成')


client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
private_key = paramiko.RSAKey.from_private_key_file(r"C:\Users\mg\.ssh\xb.pem")
username = 'root'
host = '114.55.247.56'
client.connect(host, username=username, pkey=private_key)
stdin, stdout, stderr = client.exec_command('cd /project/yun-zhong-ke/ && ./deploy_frontend.sh')
print(stdout.read().decode('utf-8'))
client.close()

# 部署完 删除 dist.zip 文件
os.remove('dist.zip')
print(datetime.datetime.now(), '前端项目部署完成')
print('耗时:', (datetime.datetime.now() - start).seconds, '秒')

